package p42_自定义条件注解;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;
import p21_参数解析器.Person;
import p41_autoconfig.ConditionalOnExists;

/**
 * 这节介绍下如何自定义条件注解
 */
@Configuration
public class _1_CustomizeCondition {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean(_1_CustomizeCondition.class);
        context.registerBean(ConfigurationClassPostProcessor.class);
        context.refresh();

        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    /**
     * 使用自定义条件注解，来判断选择是否注册bean
     * 根据类路径下有/无某个类，来选择性加载该bean
     */
    @ConditionalOnExists(className = "p42_条件装配原理.P", exists = false)
    @Bean
    public Person p() {
        return new Person();
    }
}
